package Constraints.Impl;

import Constraints.IConstraint;
import Core.ErrorMessage;
import Model.Core.IEntity;
import Model.Diagrams.CommunicationDiagram;
import Model.Draw.IEntityDraw;
import java.util.ArrayList;
import java.util.List;

/**
 * Projeto de TCC: Editor de Diagramas de comunicação BlueJ
 * @author Jose
 */
public class CommunicationDiagramConstraint implements IConstraint{

    @Override
    public List<ErrorMessage> Validate(Object obj) {
        List<ErrorMessage> errors = new ArrayList<ErrorMessage>();
        Object[] elements = (Object[])obj;
        
        CommunicationDiagram diagram = (CommunicationDiagram)elements[0];
        IEntity currentEntity = ((IEntityDraw)elements[1]).getEntity();
        String name = (String)elements[2];
        
        for(IEntity entity : diagram.getEntities()){
            if(entity.getClass() == currentEntity.getClass() && !entity.equals(currentEntity)){
                if(entity.getDescription().equalsIgnoreCase(name)){
                    errors.add(new ErrorMessage("Rename", "O nome digitado já existe. Certifique-se que os nomes são únicos."));
                }
            }
        }
        return errors.size() > 0 ? errors : null;
    }
    
}
